<?php

namespace app\partners\controller;

use app\common\controller\Adminbase;
use app\model\AgentShop;
use app\model\DistributionSetting;
use app\model\AgentAudit;
use Wechat\CommerceApp;

class Audit extends Adminbase
{
    public function index()
    {
        $model = 'AgentAudit';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list  = AgentAudit::where($map)
                ->field('*')
                ->with(['agent'])
                ->withAttr('status', function ($value) {
                    $text = [0 => '未审核', 1 => '已审核', 2 => '驳回'];
                    return $text[$value];
                })
                ->page($page, $pageSize)
                ->select();
            $total = AgentAudit::where($map)->count();

            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setDelBtn(false)
            ->setAddBtn(false)
            ->setDelAllBtn(false)
            ->setDataUrl()
            ->setFormUrl('')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'status')
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('name', '微信小店名称')
            ->addTableColumn('appid', '微信小店ID')
            ->addTableColumn('agent_id', '团长ID')
            ->addTableColumn('agent_name', '团长名称')
            ->addTableColumn('create_time', '提交时间')
            ->addTableColumn('remark', '申请备注')
            ->addTableColumn('status', '审核状态')
            ->addTableOperation()
            ->setListLayerBtn([
                ['title' => '审核', 'url' => url('form'), 'area' => "['50vw', '60vh']"]
            ])
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'AgentAudit';

        $id   = $this->request->param('id/d');
        $info = AgentAudit::get($id);
        $this->assign('id', $id);
        $this->assign('action', 2);
        if ($info['status'] == 0) {
            $info['status'] = 1;
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['status'] == 1) {
                try {
                    $shop_info = CommerceApp::getInstance()->getShopInfo($info['appid']);
                    $shop      = AgentShop::where('appid', $info['appid'])->find();
                    if (empty($shop)) {
                        $save = [
                            'appid'          => $info['appid'],
                            'name'           => $shop_info['base_info']['nickname'],
                            'image'          => $shop_info['base_info']['headimg_url'] ?? '',
                            'agent_id'       => $info['agent_id'],
                            'status'         => $shop_info['status'],
                            'product_number' => $shop_info['data_info']['product_number'] ?? 0,
                            'settle_amount'  => $shop_info['data_info']['settle_amount'] ?? 0,
                            'approved_time'  => $shop_info['approved_time'],
                        ];
                    } else {
                        $save = [
                            'status'         => $shop_info['status'],
                            'product_number' => $shop_info['data_info']['product_number'] ?? 0,
                            'settle_amount'  => $shop_info['data_info']['settle_amount'] ?? 0,
                            'approved_time'  => $shop_info['approved_time'],
                        ];
                    }
                    (new AgentShop())->save($save);
                } catch (\Exception $e) {
                    return json(['code' => -1, 'msg' => $e->getMessage()]);
                }
            }
            AgentAudit::update([
                'status'       => $data['status'],
                'audit_remark' => $data['audit_remark'],
            ], ['id' => $id]);
            return json(['code' => 0, 'msg' => '操作成功', 'data' => ['close' => 1]]);
        }

        $builder = new \app\common\controller\FormBuilder();
        $builder
            ->setModel($model)
            ->setUrl('form')
            ->setSuccessUrl()
            ->addFormItem('name', 'readonly', '微信小店名称')
            ->addFormItem('appid', 'readonly', '微信小店ID')
            ->addFormItem('remark', 'readonly', '申请备注')
            ->addFormItem('status', 'radio', '审核状态', '', [1 => '通过', 2 => '驳回'])
            ->addFormItem('audit_remark', 'text', '审核备注')
            ->setFormData($info)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }
}
